package homwork3_30;

import java.util.concurrent.*;

public  class ThreadTest2 {
    public static void main(String[] args) {
            ExecutorService es = Executors.newFixedThreadPool(2);
                es.execute(new Da());
                es.execute(new Xi());
                es.shutdown();
        }
    }

class  Da implements Runnable {
  static int sum = 25;
   int x = 0;
   int c = 0;
   @Override
   public void run() {
       FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
           @Override
           public Integer call() throws Exception {
               for(int i =1;i<=sum;i++){
                   sum--;
                   Thread.sleep(3);
                   x++;
               }
               return x;
           }
       });
       //启动线程
       new Thread(task).start();
       try {
           //得到方法执行后的返回结果
            c =  task.get();
            System.out.println(c);
       } catch (InterruptedException e) {
           e.printStackTrace();
       } catch (ExecutionException e) {
           e.printStackTrace();
       }
   }
}
class Xi implements Runnable{
  int x = 0;
  int y = 0;
    @Override
    public void run() {
        FutureTask<Integer> task = new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                for(int i =1;i<=new Da().sum;i++){

                    Thread.sleep(5);
                        x++;
                }
                    return x;
            }
        });
        //启动线程
        new Thread(task).start();
        try {
            //得到方法执行后的返回结果
            y =  task.get();
            System.out.println(y);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}